<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\common\controller\Api;
use fast\Pinyin;
use think\Config;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Home extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['topping_shop','banner_q','get_articleg','banner_s','get_article','provincial_list'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // 省份
    public function provincial_list()
    {
        $result = Db::name('provincial')->select();
        $data = [];
        $data_A = [];
        $data_B = [];
        $data_C = [];
        $data_D = [];
        $data_E = [];
        $data_F = [];
        $data_G = [];
        $data_H = [];
        $data_I = [];
        $data_J = [];
        $data_K = [];
        $data_L = [];
        $data_M = [];
        $data_N = [];
        $data_O = [];
        $data_P = [];
        $data_Q = [];
        $data_R = [];
        $data_S = [];
        $data_T = [];
        $data_U = [];
        $data_V = [];
        $data_W = [];
        $data_X = [];
        $data_Y = [];
        $data_Z = [];

        foreach ($result as $key =>$value)
        {
            if ($value['szm']=='A')
            {
                $data_A[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
           if ($value['szm']=='B')
            {
                $data_B[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='C')
            {
                $data_C[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='D')
            {
                $data_D[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='E')
            {
                $data_E[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='F')
            {
                $data_F[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='G')
            {
                $data_G[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='H')
            {
                $data_H[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='I')
            {
                $data_I[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='J')
            {
                $data_J[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='K')
            {
                $data_K[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='L')
            {
                $data_L[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='M')
            {
                $data_M[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='N')
            {
                $data_N[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='O')
            {
                $data_O[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='P')
            {
                $data_P[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='Q')
            {
                $data_Q[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='R')
            {
                $data_R[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='S')
            {
                $data_S[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='T')
            {
                $data_T[] = ['id'=>$value['id'],'name'=>$value['name']];
            }   if ($value['szm']=='U')
            {
                $data_U[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='V')
            {
                $data_V[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='W')
            {
                $data_W[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='X')
            {
                $data_X[] = ['id'=>$value['id'],'name'=>$value['name']];
            }   if ($value['szm']=='Y')
            {
                $data_Y[] = ['id'=>$value['id'],'name'=>$value['name']];
            }
            if ($value['szm']=='Z')
            {
                $data_Z[] = ['id'=>$value['id'],'name'=>$value['name']];
            }

            $data = [['letter'=>'A','data'=>$data_A],
                ['letter'=>'B','data'=>$data_B],
                ['letter'=>'C','data'=>$data_C],
                ['letter'=>'D','data'=>$data_D],
                ['letter'=>'E','data'=>$data_E],
                ['letter'=>'F','data'=>$data_F],
                ['letter'=>'G','data'=>$data_G],
                ['letter'=>'H','data'=>$data_H],
                ['letter'=>'I','data'=>$data_I],
                ['letter'=>'J','data'=>$data_G],
                ['letter'=>'K','data'=>$data_K],
                ['letter'=>'K','data'=>$data_L],
                ['letter'=>'M','data'=>$data_M],
                ['letter'=>'N','data'=>$data_N],
                ['letter'=>'O','data'=>$data_O],
                ['letter'=>'P','data'=>$data_P],
                ['letter'=>'Q','data'=>$data_Q],
                ['letter'=>'R','data'=>$data_R],
                ['letter'=>'S','data'=>$data_S],
                ['letter'=>'T','data'=>$data_T],
                ['letter'=>'U','data'=>$data_U],
                ['letter'=>'V','data'=>$data_V],
                ['letter'=>'W','data'=>$data_W],
                ['letter'=>'X','data'=>$data_X],
                ['letter'=>'Y','data'=>$data_Y],
                ['letter'=>'Z','data'=>$data_Z],
            ];

        }
        $data_arr = [];
        foreach ($data as $k => $v)
        {
            if (empty($v['data']))
            {
                unset($data[$k]);
            }else{
                $data_arr[] = $v;
            }
        }
        $this->success('返回成功', $data_arr);
    }


    //全国轮播图
    public function banner_q()
    {
        $result = Db::name('banner')->where('ex_status','1')->where('status',1)->field('id,image,type_url,type_id,type,type_id')->select();

        foreach ($result as $key => $value)
        {
            $result[$key]['image'] = cdnurl($value['image'],true);
        }
        $this->success('返回成功',$result);
    }

    /**
     * 省内轮播图
     *
     */
    public function  banner_s()
    {

        $address = $this->request->header()['address'];
        $provincial_id = get_provincial($address);
        $result = Db::name('banners')->where('ex_status','1')->where('provincial_id',$provincial_id)->where('status',1)->field('id,image,type_url,type_id,type,type_id')->select();

        foreach ($result as $key => $value)
        {
            $result[$key]['image'] = cdnurl($value['image'],true);
        }
        $this->success('返回成功',$result);
    }

    // 获取省内帖子
    public function get_article()
    {

        $address = $this->request->header()['address'];
        $provincial_id = get_provincial($address);

        $result = Db::name('article')
            ->alias('a')
            ->join('user b','a.user_id=b.id')
            ->where('a.provincial_id',$provincial_id)
            ->where('a.is_del','0')
            ->where('a.status','1')
            ->order('a.is_s_top desc,a.updatetime desc')
            ->field('a.id,a.user_id,a.content,a.updatetime,a.images,b.nickname,b.avatar,a.lx_name,a.mobile')
          ;
        $result = api_page($result);
        foreach ($result as $key => $value)
        {
            $result[$key]['updatetime'] = date('Y-m-d H:i:s',$value['updatetime']);

            $result[$key]['images'] = get_image($value['images']);
            $result[$key]['avatar'] = cdnurl($value['avatar'],true);
            if (!$value['lx_name'])
            {
                $result[$key]['lx_name'] = '';
            }
            if (!$value['mobile'])
            {
                $result[$key]['mobile'] = '';
            }
            $result[$key]['qw'] = false;
//            halt( $value['images']);
        }
        $this->success('成功',$result);
    }

    // 获取国内帖子
    public function get_articleg()
    {
        $result = Db::name('article')
            ->alias('a')
            ->join('user b','a.user_id=b.id')
//            ->where('a.provincial_id',$this->auth->provincial_id)
            ->where('a.is_del','0')
            ->where('a.status','1')
            ->where('a.is_g_top','1')
            ->order('a.is_g_top desc,a.updatetime desc')
            ->field('a.id,a.user_id,a.content,a.updatetime,a.images,b.nickname,b.avatar,a.lx_name,a.mobile')
            ->select();

        foreach ($result as $key => $value)
        {
            $result[$key]['updatetime'] = date('Y-m-d H:i:s',$value['updatetime']);

            $result[$key]['images'] = get_image($value['images']);
            $result[$key]['avatar'] = cdnurl($value['avatar'],true);
            if (!$value['lx_name'])
            {
                $result[$key]['lx_name'] = '';
            }
            if (!$value['mobile'])
            {
                $result[$key]['mobile'] = '';
            }
            $result[$key]['qw'] = false;
//            halt( $value['images']);
        }
        $this->success('成功',$result);
    }
    /**
     * 普通轮播图
     *
     */
    public function banner_p()
    {
        $result = Db::name('bannerp')->where('status',1)->field('id,image,url,type,type_id')->select();

        foreach ($result as $key => $value)
        {
            $result[$key]['image'] = cdnurl($value['image'],true);
        }
        $this->success('返回成功',$result);
    }

    // 群列表

    public function crowd_list()
    {
        $search = $this->request->post('name');
        $where = [];
        if ($search)
        {
            $where['name']=['like',"%{$search}%"];
//            $where['desc']=['like',"%{$search}%"];
        }
        $result =  Db::name('crowd')->where($where)->select();
        $this->success('成功',$result);

    }
    // 会员卡列表
    public function user_code()
    {
        $gold_code = Db::name('user_block')->where('block_type_id',1)->select();
        $diamond_code = Db::name('user_block')->where('block_type_id',2)->select();
        if ($this->auth->user_type==1)
        {
           $long_time =  $this->auth->end_time - time();
           if ($long_time>86400)
           {
             $sy_time =   floor($long_time / 86400) ;

             $money = Db::name('config')->where('id',22)->value('value');
             if (!$money)
             {
                 $this->error('系统错误');
             }
             $money = (int)$sy_time * $money;
             foreach ($diamond_code as $key => $value)
             {
                 $diamond_code[$key]['price'] = $value['price'] - $money;
             }
           }

        }

        $this->success('成功',['gold'=>$gold_code,'diamond'=>$diamond_code]);
    }

    public function pay()
    {
        $amount = 0.1;
        $type =  $this->request->request('type','wechat');
        $method = $this->request->request('method');
        $open_id = '';
        if (!$amount || $amount < 0) {
            $this->error("支付金额必须大于0");
        }

        //订单号
        $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);

        //订单标题
        $title = '订单';
        //回调链接
        $notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;

        $response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method,$open_id);

        return $response;
    }

    // 购买会员卡
    public function buy_code()
    {
        $id = $this->request->post('id');
        $user_block = Db::name('user_block')->where('id',$id)->find();

        if (!$user_block)
        {
            $this->error('购买错误');
        }

        $value = '支付成功';

        Db::startTrans();
        try {


            $log = Db::name('user_block_log')->where('user_id',$this->auth->id)->order('id','desc')->find();

//            halt([$user_block['block_type_id'],$log['user_block_type']]);
            if ($log)
            {
                if ($user_block['block_type_id'] == $log['user_block_type'])
                {
                    $end_time = $user_block['date_long'] * 86400;
                    $end_time =$this->auth->end_time + $end_time;
                    Db::name('user')->where('id',$this->auth->id)->update(['end_time'=>$end_time]);
                    Db::name('user_block_log')->insert(['user_id'=>$this->auth->id,'user_block_id'=>$user_block['id'],'user_block_type'=>$user_block['block_type_id'],'createtime'=>time(),'end_time'=>$end_time]);
                }else{
                    if ($log['user_block_type'] > $user_block['block_type_id'])
                    {
                        $this->error('不支持降级');
                    }
                    $end_time = time() + $user_block['date_long'] * 86400;

                    Db::name('user')->where('id',$this->auth->id)->update(['end_time'=>$end_time,'user_type'=>$user_block['block_type_id']]);
                    Db::name('user_block_log')->insert(['user_id'=>$this->auth->id,'user_block_id'=>$user_block['id'],'user_block_type'=>$user_block['block_type_id'],'createtime'=>time(),'end_time'=>$end_time]);
                }
            }else{
                if ($this->auth->pid !=0)
                {
                    $value = Db::name('config')->where('id',37)->value('value');

                    $p_user = Db::name('user')->where('id',$this->auth->pid)->find();
                    if ($this->auth->gm_num==0)
                    {
                        if ($p_user['tg']==1)
                        {
                            $money =round($value * $user_block['price'],2);
                            Db::name('user_money_log')->insert(['user_id'=>$p_user['id'],'money'=>$money,'before'=>$p_user['money'],'after'=>$money + $p_user['money'],'memo'=>'直推金额','createtime'=>time(),'from_user_id'=>$this->auth->id]);
                            Db::name('user')->where('id',$this->auth->pid)->setInc('money',$money);
                        }
                    }
                }
                $end_time = time() + $user_block['date_long'] * 86400;
                Db::name('user')->where('id',$this->auth->id)->update(['end_time'=>$end_time,'user_type'=>$user_block['block_type_id'],'gm_num'=>$this->auth->gm_num +1]);
                Db::name('user_block_log')->insert(['user_id'=>$this->auth->id,'user_block_id'=>$user_block['id'],'user_block_type'=>$user_block['block_type_id'],'createtime'=>time(),'end_time'=>$end_time]);
            }
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('成功',1);

    }

    // 商家入住
    public function business_rz()
    {

        if ($this->auth->user_type !=2)
        {
            $this->error('请先购买或续费钻石会员');
        }
        $user_business = Db::name('user_business')->where('user_id',$this->auth->id)->find();
        if ($user_business)
        {
            if ($user_business['status']==0)
            {
                $this->error('当前商家正在审核,请等待');
            }

        }
        $name = $this->request->post('name');
        if (!$name)
        {
            $this->error('请填写商家名称');
        }
        $code = $this->request->post('code');
        if (!$code)
        {
            $this->error('请填写纳税人识别号码');
        }
        $position= $this->request->post('position');
        if (!$position)
        {
            $this->error('请填写地址');
        }
        $detail= $this->request->post('detail');
        $license_image= $this->request->post('license_image');
        if (!$license_image)
        {
            $this->error('请上传营业执照');
        }
        $company_image= $this->request->post('company_image');
        if (!$company_image)
        {
            $this->error('请上传公司照片');
        }
        $address = $this->request->header()['address'];
        get_provincial($address);

        $longitude= $this->request->post('longitude');
        $latitude= $this->request->post('latitude');
        if ($user_business)
        {
            $id = Db::name('user_business')->where('id',$user_business['id'])->update(['user_id'=>$this->auth->id,'createtime'=>time(),'provincial_id'=>get_provincial($address),'updatetime'=>time(),'name'=>$name,'code'=>$code,'position'=>$position,'detail'=>$detail,'license_image'=>$license_image,'company_image'=>$company_image,'longitude'=>$longitude,'latitude'=>$latitude]);

        }else{
            $url = Db::name('config')->where('id',33)->value('value');
            $id = Db::name('user_business')->insertGetId(['user_id'=>$this->auth->id,'avatar'=>$url,'createtime'=>time(),'provincial_id'=>get_provincial($address),'updatetime'=>time(),'name'=>$name,'code'=>$code,'position'=>$position,'detail'=>$detail,'license_image'=>$license_image,'company_image'=>$company_image,'longitude'=>$longitude,'latitude'=>$latitude]);
            Db::name('user')->where('id',$this->auth->id)->update(['shop_id'=>$id]);
        }
        $this->success('入驻成功，等待审核 ',1);
    }


    // 商品类型
    public function getShopType()
    {
       $result =  Db::name('shop_type')->select();
       $this->success('成功',$result);
    }

    // 发布商品
    public function upload_shop()
    {
        $image = $this->request->post('image');
        $name = $this->request->post('name');
        $type = $this->request->post('type');
        $price = $this->request->post('price');
        $num = $this->request->post('num');
        $detail = $this->request->post('detail');
        $detail_image = $this->request->post('detail_image');
        $banner = $this->request->post('banner');
        if (!$image or !$name or !$type or  !$price or !$num or !$detail or  !$banner)
        {
            $this->error('请完善信息');
        }

        if ($this->auth->is_name==0)
        {
            $this->error('请先实名');
        }
        $shenh = Db::name('config')->where('id',20)->value('value');
        $status = 0;
        if ($shenh==0)
        {
            $status = 1;
        }
        Db::startTrans();
        try {

            Db::name('shop')->insert(['user_id'=>$this->auth->id,'shop_id'=>$this->auth->shop_id,'status'=>$status,'createtime'=>time(),'updatetime'=>time(),'provincial_id'=>$this->auth->provincial_id,'image'=>$image,'banner_image'=>$banner,'name'=>$name,'shop_type_id'=>$type,'price'=>$price,'num'=>$num,'detail'=>$detail,'detail_image'=>$detail_image]);
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }

    $this->success('成功',1);
    }
    // 发布公告
    public function release_notice()
    {
        $content = $this->request->post('content');
        Db::name('user_business')->where('user_id',$this->auth->id)->update(['notice'=>$content]);
        $this->success('更新成功',1);
    }
    // 上下架接口
    public function update_status()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');

        $result = Db::name('shop')->where('id',$id)->find();
        if ($result['status']!=1)
        {
            $this->error('审核未通过不允许上架');
        }
        if (!$id)
        {
            $this->error('缺少参数');
        }
        $status = 0;
        if ($type==1)
        {
            $status=1;
        }
        Db::name('shop')->where('id',$id)->update(['is_status'=>$status]);
        $this->success('成功',1);
    }

    // 删除商品
    public function shop_del()
    {
        $id = $this->request->post('id');
        if (!$id)
        {
            $this->error('缺少参数');
        }
        Db::name('shop')->where('id',$id)->delete();
        $this->success('成功',1);
    }


    // 商品列表
    public function shop_list()
    {
        $search = $this->request->post('name');
        $is_status = $this->request->post('is_status','0');// 0=在售,1=待上架
        $where['a.is_status'] = $is_status==1?'0':'1';
        if ($is_status==1)
        {

        }else{
            $where['a.status']='1';
        }
//        $where = [];
        if ($search)
        {
            $where['a.name']=['like',"%{$search}%"];
//            $where['desc']=['like',"%{$search}%"];
        }
//        halt($where);
        $result = Db::name('shop')
            ->alias('a')
            ->join('shop_type b','a.shop_type_id=b.id')
//            ->where('status',2)
            ->where($where)
//            ->where('a.is_status','1')
            ->where('a.user_id',$this->auth->id)
            ->field('a.id,a.image,a.name,b.name as type_name,a.price,a.num,a.status,enmo as memo')
        ;

        $result = api_page($result);
        foreach ($result as $key =>$value)
        {
            $result[$key]['status_text'] = $value['status']==0?'待审核':($value['status']==1?'审核成功':'审核失败');
        }
        $this->success('成功',$result);
    }



    // 查看上架商品列表
    public function user_shop_list()
    {


        $search = $this->request->post('name');
        $business_id = $this->request->post('id');
//        $business_type = $this->request->post('business_type');
        $shop_type_id = $this->request->post('type');
        $is_status = $this->request->post('is_status','0');// 0=在售,1=待上架
        $where['a.is_status'] = $is_status==1?'0':'1';
        if (!$business_id)
        {
            $this->error('缺少商家id');
        }
        $user_business = Db::name('user_business')->where('id',$business_id)->find();
        if (!$user_business)
        {
            $this->error('当前商家不存在');
        }


        if ($is_status==1)
        {

        }else{
            $where['a.status']='1';
        }
//        $where = [];
        if ($search)
        {
            $where['a.name']=['like',"%{$search}%"];
//            $where['desc']=['like',"%{$search}%"];
        }
        if ($shop_type_id)
        {
            $where['a.shop_type_id']=$shop_type_id;
        }
//        halt($where);
        $result = Db::name('shop')
            ->alias('a')
            ->join('shop_type b','a.shop_type_id=b.id')
//            ->where('status',2)
            ->where($where)
//            ->where('a.is_status','1')
            ->where('a.user_id',$user_business['user_id'])
            ->field('a.id,a.image,a.name,b.name as type_name,a.price,a.num,a.status,enmo as memo')
        ;

        $result = api_page($result);
        foreach ($result as $key =>$value)
        {
            $result[$key]['status_text'] = $value['status']==0?'待审核':($value['status']==1?'审核成功':'审核失败');
        }
        $this->success('成功',$result);
    }

    // 获取商家分类
    public function get_shop_type()
    {
        $business_id = $this->request->post('id');
        $user_business = Db::name('user_business')->where('id',$business_id)->find();
        $where['a.status']='1';
        $result = Db::name('shop')
            ->alias('a')
            ->join('shop_type b','a.shop_type_id=b.id')
//            ->where('status',2)
            ->where($where)
            ->group('a.shop_type_id')
            ->where('a.user_id',$user_business['user_id'])
            ->field('b.id,b.name')
            ->select();
        ;
        $this->success('成功',$result);
    }



    // 商品详情
    public function shop_detail()
    {
        $id = $this->request->post('id');
        if (!$id)
        {
            $this->error('缺少参数');
        }
        $result = Db::name('shop')->where('id',$id)->find();
        $result['type_name'] =  Db::name('shop_type')->where('id',$result['shop_type_id'])->value('name');
        if (!$result)
        {
            $this->error('商品错误');
        }
        $this->success('成功',$result);
    }
    //

    // 商品编辑
    public function shop_edit()
    {
        $id = $this->request->post('id');
        $image = $this->request->post('image');
        $name = $this->request->post('name');
        $type = $this->request->post('type');
        $price = $this->request->post('price');
        $num = $this->request->post('num');
        $detail = $this->request->post('detail');
        $detail_image = $this->request->post('detail_image');
        $banner = $this->request->post('banner');
        if (!$image or !$name or !$type or  !$price or !$num or !$detail or !$detail_image or !$banner or !$id)
        {
            $this->error('请完善信息');
        }
        Db::startTrans();
        try {
            $shenh = Db::name('config')->where('id',20)->value('value');
            $status = 0;
            if ($shenh==0)
            {
                $status = 1;
            }
            Db::name('shop')->where('id',$id)->update(['user_id'=>$this->auth->id,'status'=>$status,'is_status'=>0,'provincial_id'=>$this->auth->provincial_id,'image'=>$image,'banner_image'=>$banner,'name'=>$name,'shop_type_id'=>$type,'price'=>$price,'num'=>$num,'detail'=>$detail,'detail_image'=>$detail_image]);
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功',1);
    }

    // 获取商品刷新需要支付的金额
    public function refresh_shop_money()
    {
        $type = $this->request->post('type'); // 刷新类型 1= 微信支付,2=积分支付,3=混合支付,4 =刷新次数支付

        // 刷新金额
        $refresh = Db::name('config')->where('id',30)->value('value');
        // 积分兑换比例
        $refresh2 = Db::name('config')->where('id',26)->value('value');

        if ($type=='1')
        {
            $this->success('成功',['money'=>$refresh,'score'=>0]);
        }elseif ($type==2)
        {
            $score = $refresh * $refresh2;
            if ($this->auth->score < $score)
            {
                $this->success('成功','积分不足');
            }
            $this->success('成功',['money'=>0,'score'=>$score]);
        }elseif($type==3){
            $score = $refresh /2 *  $refresh2;
            $money = $refresh /2;
            if ($this->auth->score < $score)
            {
                $this->error('成功','积分不足');
            }
            $this->success('成功',['money'=>$money,'score'=>$score]);
//            echo '支付成功';
//            Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
        }else{
            // 扣除刷新次数逻辑
            if ($this->auth->user_type!=2)
            {
                $this->error('当前等级没有刷新次数');
            }

            $shuaxin_num = 0;

            $user_num_time =  Db::name('user_num_time')->where('user_id',$this->auth->id)->where('month',date('m',time()))->find();
            if ($user_num_time)
            {
                $shuaxin_num = $user_num_time['refresh_num'];
            }else{
                Db::name('user_num_time')->insert(['user_id'=>$this->auth->id,'month'=>date('m',time()),'refresh_num'=>20]);
                $shuaxin_num = 20;
            }
            $this->success('成功',['num'=>$shuaxin_num]);
        }
        $this->success('成功',1);
    }

    // 刷新
    public function  refresh_shop()
    {
        $type = $this->request->post('type'); // 刷新类型 1= 微信支付,2=积分支付,3=混合支付,4 =刷新次数支付
        $shop_id= $this->request->param('article_id');

        // 刷新金额
        $refresh = Db::name('config')->where('id',30)->value('value');
        // 积分兑换比例
        $refresh2 = Db::name('config')->where('id',27)->value('value');

        if ($type=='1')
        {
            $money =  $refresh;
//            echo '支付成功';
        }elseif ($type==2)
        {
            $score = $refresh * $refresh2;
            if ($this->auth->score < $score)
            {
                $this->error('积分余额不足');
            }
            Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
        }elseif($type==3){

            $score = $refresh /2 *  $refresh2;
            $money = $refresh /2;
            echo '支付成功';
            Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
        }else{
            // 扣除刷新次数逻辑
            if ($this->auth->user_type!=2)
            {
                $this->error('当前等级没有刷新次数');
            }
            $user_num_time =  Db::name('user_num_time')->where('user_id',$this->auth->id)->where('month',date('m',time()))->find();

            if ($user_num_time)
            {
                if ($user_num_time['refresh_num']==0)
                {
                    $this->error('当前刷新次数已用完');
                }
                Db::name('user_num_time')->where('user_id',$this->auth->id)->where('month',date('m',time()))->setDec('refresh_num');
            }else{
                Db::name('user_num_time')->insert(['user_id'=>$this->auth->id,'month'=>date('m',time()),'refresh_num'=>19]);
            }
        }
        Db::name('shop')->where('id',$shop_id)->update(['updatetime'=>time()]);

        $this->success('成功',date('Y-m-d H:i:s',time()));
    }

    // 商品置顶金额列表


    // 置顶金额列表
    public function top_shop_list()
    {
        $type = $this->request->post('type');
        if ($type==2)
        {
            $result1 = Db::name('topping_shop')->where('city_type',1)->where('type',1)->field('id,name,price')->select();

        }else{
            $result1 = Db::name('topping_shop')->where('city_type',2)->where('type',1)->field('id,name,price')->select();

        }
        foreach ($result1 as $key => $value)
        {
            $result1[$key]['aaa'] = false;
        }
        $this->success('成功',$result1);
    }


    // 置顶金额
    public function shop_top_money()
    {
        $top_id     = $this->request->param('top_id');
        if (!$top_id)
        {
            $this->error('请先选择购买期限');
        }
        $type     = $this->request->param('type'); // 1= 微信支付,2=积分支付,3=混合支付
        if (!$type)
        {
            $this->error('缺少参数');
        }
        $result = Db::name('topping_shop')->where('id',$top_id)->find();
        if (!$result){
            $this->error('系统出错');
        }

        // 积分兑换比例
        $refresh2 = Db::name('config')->where('id',26)->value('value');
        if ($result['city_type']=='1')
        {
            $num = Db::name('config')->where('id',31)->value('value');
            $count = Db::name('shop')->where('is_g_top',1)->count();
            if ($count >=$num)
            {
                $this->error('置顶位置不足');
            }
            if ($type=='1')
            {
                $money = $result['price'];
                $this->success('',['money'=>$money,'score'=>0]);
            }elseif($type=='2'){
                $score = $result['price'] * $refresh2;
                if ($score > $this->auth->score)
                {
                    $this->success('','积分不足');
                }
                $this->success('',['money'=>0,'score'=>$score]);
            }else{
                $score =  $result['price'] /2 *  $refresh2;
                $money =  $result['price'] /2;
                if ($this->auth->score < $score)
                {
                    $this->error('积分不足');
                }
                $this->success('',['money'=>$money,'score'=>$score]);
            }

        }elseif ($result['city_type']=='2')
        {

            $num = Db::name('config')->where('id',31)->value('value');

            $count = Db::name('shop')->where('is_s_top',1)->count();

            if ($count >=$num)
            {
                $this->error('置顶位置不足');
            }
            if ($type=='1')
            {
                $money = $result['price'];
                $this->success('成功',['money'=>$money,'score'=>0]);
            }elseif($type=='2'){
                $score = $result['price'] * $refresh2;
                if ($score > $this->auth->score)
                {
                    $this->success('成功','积分不足');
                }
                $this->success('成功',['money'=>0,'score'=>$score]);
            }else{
                $score =  $result['price'] /2 *  $refresh2;
                $money =  $result['price'] /2;
                if ($this->auth->score < $score)
                {
                    $this->error('积分不足');
                }
                $this->success('成功',['money'=>$money,'score'=>$score]);
            }
        }
    }

    // 置顶 置顶剩余时间需要定时器判断
    public function topping_shop()
    {
        $top_id     = $this->request->param('top_id');
        if (!$top_id)
        {
            $this->error('请先选择购买期限');
        }
        $article_id = $this->request->param('article_id');
        $type = $this->request->param('type');
        if (!$type)
        {
            $this->error('缺少参数');
        }
        $result = Db::name('topping_shop')->where('id',$top_id)->find();
        if (!$result){
            $this->error('系统出错');
        }
        $end_time = $result['num']* 86400;
        $article = Db::name('shop')->where('id',$article_id)->find();
        $update = [];


        Db::startTrans();
        try {

            // 积分兑换比例
            $refresh2 = Db::name('config')->where('id',26)->value('value');
            if ($result['city_type']=='1')
            {
                $num = Db::name('config')->where('id',31)->value('value');

                $count = Db::name('shop')->where('is_g_top',1)->count();

                if ($count >=$num)
                {
                    $this->error('置顶位置不足');
                }
                if ($type=='1')
                {
                    $money = $result['price'];
                    $this->error('暂未开放');
                }elseif($type=='2'){
                    $score = $result['price'] * $refresh2;
                    if ($score > $this->auth->score)
                    {
                        $this->success('成功','积分不足');
                    }
                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }else{
                    $score =  $result['price'] /2 *  $refresh2;
                    $money =  $result['price'] /2;
                    if ($this->auth->score < $score)
                    {
                        $this->error('积分不足');
                    }
                    $this->error('暂未开放');

                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }
                if ($article['g_endtime'] != 0)
                {
                    $end_time = $article['g_endtime'] + $end_time;
                }else{
                    $end_time = time() + $end_time;
                }
                $update = ['g_endtime'=>$end_time,'is_g_top'=>1];
            }elseif ($result['city_type']=='2')
            {
                $num = Db::name('config')->where('id',32)->value('value');

                $count = Db::name('shop')->where('is_s_top',1)->count();

                if ($count >=$num)
                {
                    $this->error('置顶位置不足');
                }

                if ($count >=$num)
                {
                    $this->error('置顶位置不足');
                }
                if ($type=='1')
                {
                    $money = $result['price'];
                    $this->error('暂未开放');
                }elseif($type=='2'){
                    $score = $result['price'] * $refresh2;
                    if ($score > $this->auth->score)
                    {
                        $this->success('成功','积分不足');
                    }
                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }else{
                    $score =  $result['price'] /2 *  $refresh2;
                    $money =  $result['price'] /2;
                    if ($this->auth->score < $score)
                    {
                        $this->error('积分不足');
                    }
                    $this->error('暂未开放');

                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }

                if ($article['s_endtime'] != 0)
                {
                    $end_time = $article['s_endtime'] + $end_time;
                }else{
                    $end_time = time() + $end_time;
                }
                $update = ['s_endtime'=>$end_time,'is_s_top'=>1];
            }
            Db::name('shop')->where('id',$article_id)->update($update);
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('成功',1);
    }



    // 发布帖子
    public function release()
    {
        $user_type = $this->auth->user_type;
        if ($user_type==0)
        {
            $this->error('当前账号没有权限 请购买会员');
        }
        if ($this->auth->is_name==0)
        {
            $this->error('请先实名');
        }
        $address = $this->request->header()['address'];
        $provincial_id = get_provincial($address);
//        if (!$this->auth->provincial_id)
//        {
//            $this->error('请先选择地址');
//        }
        $count = Db::name('article')->where('user_id',$this->auth->id)->whereTime('createtime','today')->count();
        if ($user_type ==1)
        {
            if ($count >=2)
            {
                $this->error('每日发帖2次');
            }
        }
        if ($user_type ==2)
        {
            if ($count >=4)
            {
                $this->error('每日发帖4次');
            }
        }
        $shenh = Db::name('config')->where('id',20)->value('value');
        $status = 0;
        if ($shenh==0)
        {
            $status = 1;
        }

//        $name = $this->request->post('name')??$this->error('请输入标题');
        $content = $this->request->post('content');
        if (!$content)
        {
            $this->error('请输入内容');
        }
        $images= $this->request->post('images','');

//        if (!$images)
//        {
//           $this->error('请上传图片');
//        }
        $lx_name= $this->request->post('lx_name');
        $mobile= $this->request->post('mobile');
        if (strlen($content)>360)
        {
            $this->error('超出限定字数');
        }
        Db::name('article')->insert(['user_id'=>$this->auth->id,'provincial_id'=>$provincial_id,'content'=>$content,'images'=>$images,'status'=>$status,'createtime'=>time(),'updatetime'=>time(),'lx_name'=>$lx_name,'mobile'=>$mobile]);

        $score= Db::name('config')->where('id',21)->value('value');
        if ($this->auth->user_type==2)
        {
            $score = $score *2;
            Db::name('user_score_log')->insert(['user_id'=>$this->auth->id,'score'=>$score,'before'=>$this->auth->score,'after'=>$this->auth->score+ $score,'createtime'=>time(),'memo'=>'发布帖子获得积分']);
            Db::name('user')->where('id',$this->auth->id)->setInc('score',$score);
        }else{
            Db::name('user_score_log')->insert(['user_id'=>$this->auth->id,'score'=>$score,'before'=>$this->auth->score,'after'=>$this->auth->score+ $score,'createtime'=>time(),'memo'=>'发布帖子获得积分']);
            Db::name('user')->where('id',$this->auth->id)->setInc('score',$score);
        }
        $this->success('成功',1);
    }

    // 我的发布
    public function my_release()
    {
        $result = Db::name('article')
            ->alias('a')
            ->join('user b','a.user_id=b.id')
            ->where('a.is_del','0')

            ->order('a.is_s_top desc,a.is_g_top desc,a.updatetime desc')
//            ->order('a.updatetime desc')

            ->field('a.id,a.user_id,a.content,a.updatetime,a.images,b.nickname,b.avatar,a.lx_name,a.mobile,a.memo,a.status')
            ->where('user_id',$this->auth->id);
        $result = api_page($result);
        foreach ($result as $key => $value)
        {
            $result[$key]['updatetime'] = date('Y-m-d H:i:s',$value['updatetime']);

            $result[$key]['images'] = get_image($value['images']);
            $result[$key]['avatar'] = cdnurl($value['avatar'],true);
            if (!$value['lx_name'])
            {
                $result[$key]['lx_name'] = '';
            }
            if (!$value['mobile'])
            {
                $result[$key]['mobile'] = '';
            }
            $result[$key]['qw'] = false;
//            halt( $value['images']);
        }
        $this->success('成功',$result);
    }

    // 置顶金额列表
    public function top_list()
    {
        $type = $this->request->post('type');
        if ($type==2)
        {
            $result1 = Db::name('topping')->where('city_type',1)->where('type',1)->field('id,name,price')->select();

        }else{
            $result1 = Db::name('topping')->where('city_type',2)->where('type',1)->field('id,name,price')->select();

        }
        foreach ($result1 as $key => $value)
        {
            $result1[$key]['aaa'] = false;
        }
        $this->success('成功',$result1);
    }

    // 置顶金额
    public function top_money()
    {
        $top_id     = $this->request->param('top_id');
        if (!$top_id)
        {
            $this->error('请先选择购买期限');
        }
        $type     = $this->request->param('type'); // 1= 微信支付,2=积分支付,3=混合支付
        if (!$type)
        {
            $this->error('缺少参数');
        }
        $result = Db::name('topping')->where('id',$top_id)->find();
        if (!$result){
            $this->error('系统出错');
        }


        // 积分兑换比例
        $refresh2 = Db::name('config')->where('id',26)->value('value');
        if ($result['city_type']=='1')
        {
            $num = Db::name('config')->where('id',25)->value('value');
            $count = Db::name('article')->where('is_g_top',1)->count();
            if ($count >=$num)
            {
                $this->error('置顶位置不足');
            }
            if ($type=='1')
            {
                $money = $result['price'];
                $this->success('',['money'=>$money,'score'=>0]);
            }elseif($type=='2'){
                $score = $result['price'] * $refresh2;
                if ($score > $this->auth->score)
                {
                    $this->success('','积分不足');
                }
                $this->success('',['money'=>0,'score'=>$score]);
            }else{
                $score =  $result['price'] /2 *  $refresh2;
                $money =  $result['price'] /2;
                if ($this->auth->score < $score)
                {
                    $this->error('积分不足');
                }
                $this->success('',['money'=>$money,'score'=>$score]);
            }

        }elseif ($result['city_type']=='2')
        {

            $num = Db::name('config')->where('id',24)->value('value');

            $count = Db::name('article')->where('is_s_top',1)->count();

            if ($count >=$num)
            {
                $this->error('置顶位置不足');
            }
            if ($type=='1')
            {
                $money = $result['price'];
                $this->success('成功',['money'=>$money,'score'=>0]);
            }elseif($type=='2'){
                $score = $result['price'] * $refresh2;
                if ($score > $this->auth->score)
                {
                    $this->success('成功','积分不足');
                }
                $this->success('成功',['money'=>0,'score'=>$score]);
            }else{
                $score =  $result['price'] /2 *  $refresh2;
                $money =  $result['price'] /2;
                if ($this->auth->score < $score)
                {
                    $this->error('积分不足');
                }
                $this->success('成功',['money'=>$money,'score'=>$score]);
            }
        }
    }

    // 置顶 置顶剩余时间需要定时器判断
    public function topping()
    {
        $top_id     = $this->request->param('top_id');
        if (!$top_id)
        {
            $this->error('请先选择购买期限');
        }
        $article_id = $this->request->param('article_id');
        $type = $this->request->param('type');
        if (!$type)
        {
            $this->error('缺少参数');
        }
        $result = Db::name('topping')->where('id',$top_id)->find();
        if (!$result){
            $this->error('系统出错');
        }
        $end_time = $result['num']* 86400;
        $article = Db::name('article')->where('id',$article_id)->find();
        $update = [];


        Db::startTrans();
        try {

            // 积分兑换比例
            $refresh2 = Db::name('config')->where('id',26)->value('value');
            if ($result['city_type']=='1')
            {
                $num = Db::name('config')->where('id',25)->value('value');

                $count = Db::name('article')->where('is_g_top',1)->count();

                if ($count >=$num)
                {
                    $this->error('置顶位置不足');
                }
                if ($type=='1')
                {
                    $money = $result['price'];
                    $this->error('暂未开放');
                }elseif($type=='2'){
                    $score = $result['price'] * $refresh2;
                    if ($score > $this->auth->score)
                    {
                        $this->success('成功','积分不足');
                    }
                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }else{
                    $score =  $result['price'] /2 *  $refresh2;
                    $money =  $result['price'] /2;
                    if ($this->auth->score < $score)
                    {
                        $this->error('积分不足');
                    }
                    $this->error('暂未开放');

                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }
                if ($article['g_endtime'] != 0)
                {
                    $end_time = $article['g_endtime'] + $end_time;
                }else{
                    $end_time = time() + $end_time;
                }
                $update = ['g_endtime'=>$end_time,'is_g_top'=>1];
            }elseif ($result['city_type']=='2')
            {
                $num = Db::name('config')->where('id',24)->value('value');

                $count = Db::name('article')->where('is_s_top',1)->count();

                if ($count >=$num)
                {
                    $this->error('置顶位置不足');
                }

                if ($count >=$num)
                {
                    $this->error('置顶位置不足');
                }
                if ($type=='1')
                {
                    $money = $result['price'];
                    $this->error('暂未开放');
                }elseif($type=='2'){
                    $score = $result['price'] * $refresh2;
                    if ($score > $this->auth->score)
                    {
                        $this->success('成功','积分不足');
                    }
                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }else{
                    $score =  $result['price'] /2 *  $refresh2;
                    $money =  $result['price'] /2;
                    if ($this->auth->score < $score)
                    {
                        $this->error('积分不足');
                    }
                    $this->error('暂未开放');

                    Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
                }

                if ($article['s_endtime'] != 0)
                {
                    $end_time = $article['s_endtime'] + $end_time;
                }else{
                    $end_time = time() + $end_time;
                }
                $update = ['s_endtime'=>$end_time,'is_s_top'=>1];
            }
            Db::name('article')->where('id',$article_id)->update($update);
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }



        $this->success('成功',1);
    }

    public function update_article()
    {

        Db::startTrans();
        try {
            Db::name('article')->whereTime('s_endtime','today')->update(['s_endtime'=>0,'is_g_top'=>0]);
            Db::name('article')->whereTime('g_endtime','today')->update(['g_endtime'=>0,'is_s_top'=>0]);
            Db::name('shop')->whereTime('g_endtime','today')->update(['g_endtime'=>0,'is_s_top'=>0]);
            Db::name('shop')->whereTime('s_endtime','today')->update(['s_endtime'=>0,'is_s_top'=>0]);
            Db::name('user')->where('user_type','1')->whereTime('end_time','today')->update(['end_time'=>0,'user_type'=>0]);
            $ids = Db::name('user')->where('user_type','2')->whereTime('end_time','today')->column('id');
            if (!$ids)
            {
                Db::name('user')->where('id','in',$ids)->update(['end_time'=>0,'user_type'=>0]);
                Db::name('user_business')->where('user_id','in',$ids)->update(['is_status'=>0]);
                Db::name('shop')->where('user_id','in',$ids)->update(['is_status'=>0]);
            }
            Db::name('user_business')->whereTime('g_endtime','today')->update(['g_endtime'=>0,'is_s_top'=>0]);
            Db::name('user_business')->whereTime('s_endtime','today')->update(['s_endtime'=>0,'is_s_top'=>0]);
            Db::commit();
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('成功',1);
    }

    // 获取刷新需要支付的金额
    public function refresh_money()
    {
        $type = $this->request->post('type'); // 刷新类型 1= 微信支付,2=积分支付,3=混合支付,4 =刷新次数支付

        // 刷新金额
        $refresh = Db::name('config')->where('id',27)->value('value');
        // 积分兑换比例
        $refresh2 = Db::name('config')->where('id',26)->value('value');

        if ($type=='1')
        {
            $this->success('成功',['money'=>$refresh,'score'=>0]);
        }elseif ($type==2)
        {
            $score = $refresh * $refresh2;
            if ($this->auth->score < $score)
            {
                $this->success('成功','积分不足');
            }
            $this->success('成功',['money'=>0,'score'=>$score]);
        }elseif($type==3){
            $score = $refresh /2 *  $refresh2;
            $money = $refresh /2;
            if ($this->auth->score < $score)
            {
                $this->error('成功','积分不足');
            }
            $this->success('成功',['money'=>$money,'score'=>$score]);
//            echo '支付成功';
//            Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
        }else{
            // 扣除刷新次数逻辑
            if ($this->auth->user_type!=2)
            {
                $this->error('当前等级没有刷新次数');
            }

            $shuaxin_num = 0;

            $user_num_time =  Db::name('user_num_time')->where('user_id',$this->auth->id)->where('month',date('m',time()))->find();
            if ($user_num_time)
            {
                $shuaxin_num = $user_num_time['refresh_num'];
            }else{
                Db::name('user_num_time')->insert(['user_id'=>$this->auth->id,'month'=>date('m',time()),'refresh_num'=>20]);
                $shuaxin_num = 20;
            }
            $this->success('成功',['num'=>$shuaxin_num]);
        }

        $this->success('成功',1);
    }

    //  删除
    public function  article_del()
    {
        $id = $this->request->param('id');
        Db::name('article')->where('id',$id)->update(['is_del'=>1]);
        $this->success('删除成功',1);
    }


    // 刷新
    public function  refresh()
    {
        $type = $this->request->post('type'); // 刷新类型 1= 微信支付,2=积分支付,3=混合支付,4 =刷新次数支付
        $article_id = $this->request->param('article_id');

        // 刷新金额
        $refresh = Db::name('config')->where('id',26)->value('value');
        // 积分兑换比例
        $refresh2 = Db::name('config')->where('id',27)->value('value');

        if ($type=='1')
        {
            $money =  $refresh;
//            echo '支付成功';
        }elseif ($type==2)
        {
            $score = $refresh * $refresh2;
            if ($this->auth->score < $score)
            {
                $this->error('积分余额不足');
            }
            Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
        }elseif($type==3){

            $score = $refresh /2 *  $refresh2;
            $money = $refresh /2;
            echo '支付成功';
            Db::name('user')->where('id',$this->auth->id)->setDec('score',$score);
        }else{
            // 扣除刷新次数逻辑
            if ($this->auth->user_type!=2)
            {
                $this->error('当前等级没有刷新次数');
            }
            $user_num_time =  Db::name('user_num_time')->where('user_id',$this->auth->id)->where('month',date('m',time()))->find();

            if ($user_num_time)
            {
                if ($user_num_time['refresh_num']==0)
                {
                    $this->error('当前刷新次数已用完');
                }
                Db::name('user_num_time')->where('user_id',$this->auth->id)->where('month',date('m',time()))->setDec('refresh_num');
            }else{
                Db::name('user_num_time')->insert(['user_id'=>$this->auth->id,'month'=>date('m',time()),'refresh_num'=>19]);
            }
        }
        Db::name('article')->where('id',$article_id)->update(['updatetime'=>time()]);
        $this->success('成功',date('Y-m-d H:i:s',time()));
    }






    /**
     * 计算两组经纬度坐标 之间的距离
     * params ：lat1 纬度1； lng1 经度1； lat2 纬度2； lng2 经度2； len_type （1:m or 2:km);
     * return m or km
     */
    public function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) {

        $radLat1 = $lat1 * PI / 180.0;
        $radLat2 = $lat2 * PI / 180.0;
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * PI / 180.0) - ($lng2 * PI / 180.0);
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $s = $s * EARTH_RADIUS;
        $s = round($s * 1000);
        if ($len_type > 1)
        {
            $s /= 1000;
        }
        return round($s, $decimal);
    }











    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
